package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.comment.domain.TravelComment;
import cn.wolfcode.luowowo.comment.repository.ITravelCommentRepository;
import cn.wolfcode.luowowo.comment.service.ITravelCommentService;
import cn.wolfcode.luowowo.common.query.TravelCommentQueryObject;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class TravelCommentServiceImpl implements ITravelCommentService{
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private ITravelCommentRepository dao;
    public TravelComment save(TravelComment travelComment) {
        if(travelComment.getId() == null){

            travelComment.setCreateTime(new Date());
        }
        //关联评论
        if(travelComment.getRefComment() != null && StringUtils.hasLength(travelComment.getRefComment().getId())){
          TravelComment comment = this.get(travelComment.getRefComment().getId());
          travelComment.setRefComment(comment);
        }
        dao.save(travelComment);
        return travelComment;
    }

    public Page<TravelComment> query(TravelCommentQueryObject qo) {
        //封装查询条件
        Query query = new Query();
        //设置参数
        Criteria criteria = null;
        if(qo.getTravelId() != -1){
            criteria = Criteria.where("travelId").is(qo.getTravelId());
        }else{
            criteria = Criteria.where("travelId").ne(qo.getTravelId());
        }
        query.addCriteria(criteria);
        //分页对象
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize());
        query.with(pageable);
        //排序
        query.with(new Sort(Sort.Direction.ASC,"createTime"));
        //查询总条数
        long total = mongoTemplate.count(query,TravelComment.class);
        if(total == 0){
            return Page.empty();
        }
        //查询所有的记录数
        List<TravelComment> list = mongoTemplate.find(query, TravelComment.class);
        return new PageImpl<TravelComment>(list,pageable,total);
    }

    public TravelComment get(String id) {
        Optional<TravelComment> optional = dao.findById(id);
        TravelComment comment = optional.get();
        return comment;
    }

   /* public boolean thumup(String toid, Long fromid) {
        Optional<TravelComment> optional = dao.findById(toid);
        TravelComment comment = null;
        if(optional != null){
             comment = optional.get();
        }
        boolean flag = false;
        List<Long> thumbuplist = comment.getThumbuplist();
        if(!thumbuplist.contains(fromid)){
            //点赞
            comment.setThumbupnum(comment.getThumbupnum() +1);
            thumbuplist.add(fromid);
            flag = true;
        }else{
            //取消点赞
            comment.setThumbupnum(comment.getThumbupnum() - 1);
            thumbuplist.remove(fromid);
            flag = false;
        }
        dao.save(comment);
        return flag;
    }*/
}
